package satirGame;
import java.awt.GridLayout;

import satirGame.MapNode;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Map extends JPanel {
	/* Fields */
	private int nodeNum;
	private int mapWidth;
	private int mapHeight;
	private int eventNum;
	private int[][] eventPos;
	private int[] eventType;
	private ImageIcon EventNodeStyle;
	private ImageIcon[] playerNodeStyle;
	public int[][] mapnode;
	public MapNode[][] nodesInstance;
	private int[] availableStep;
	// public Button mapNodeBtn[];
	// public ImageIcon nodeIcon[];
	private int playerLocationX = 2;
	private int playerLocationY = 3;

	public Map(int n, int w, int h) {
		int nodeType;
		nodeNum = n;
		mapWidth = w;
		mapHeight = h;
		setLayout(new GridLayout(mapHeight, mapWidth));
		mapnode = new int[mapHeight][mapWidth];
		eventPos = new int[mapHeight][mapWidth];
		nodesInstance = new MapNode[mapHeight][mapWidth];
		EventNodeStyle=new ImageIcon("img/e.png");
		playerNodeStyle=new ImageIcon[6];
		for (int i = 0; i < 6; i++) {
			playerNodeStyle[i]=new ImageIcon("img/c"+i+".png");
		}
		
		// generate each map node {0:goRight, 1: goDown, 2:goUp}
		int pIndex=0;
		for (int iHeight = 0; iHeight < mapHeight; iHeight++) {
			for (int jWidth = 0; jWidth < mapWidth; jWidth++) {
				if (iHeight == 0) {
					nodeType = (int) (Math.random() * 2);
				}else if (iHeight == (mapHeight - 1)) { // last column
					if ( mapnode[iHeight- 1][jWidth] == 1)// 
						nodeType = 2;
					else
					nodeType = 0; // this node must go up
				}else if ( mapnode[iHeight- 1][jWidth] == 1) { // 
					nodeType = 2;
				}  else { // this node can be up or right go
					nodeType = (int) (Math.random() * 2);
				}
				
				mapnode[iHeight][jWidth] = nodeType; // node type for current node
				nodesInstance[iHeight][jWidth]= new MapNode(iHeight,jWidth,nodeType,pIndex);
				if(jWidth!=0){
					eventPos[iHeight][jWidth] = pIndex%2;
					if(eventPos[iHeight][jWidth]==1){
						nodesInstance[iHeight][jWidth].setIconState(EventNodeStyle);
					}
				}else
					eventPos[iHeight][jWidth]=0;
				add(nodesInstance[iHeight][jWidth]);
				pIndex++;
			}
		}
		// drawMap();

	}

	public void drawMap() {
		int initPos=Stage.playerArray.get(Stage.getCurPlayer()).getCurPos();
		int totalStep=Stage.playerArray.get(Stage.getCurPlayer()).getStep();
		availableStep=new int[3];
		availableStep[0]=initPos+1;
		availableStep[1]=initPos-1;		
		if (mapnode[initPos/mapWidth][initPos%mapWidth]==1) {
			availableStep[2]=initPos+10;
		}else if(mapnode[initPos/mapWidth][initPos%mapWidth]==2){
			availableStep[2]=initPos-10;
		}else {
			availableStep[2]=-1;
		}
		for (int i = 0; i < 3; i++) {
			if (availableStep[i]>(-1)) {
				int bgValue=mapnode[availableStep[i]/mapWidth][availableStep[i]%mapWidth]+3;
				nodesInstance[availableStep[i]/mapWidth][availableStep[i]%mapWidth].setBackgroundImage(bgValue);
			}
		}
	}

	public void resetMap() {
		for (int i = 0; i < 3; i++) {
			if (availableStep[i]>(-1)) {
				int bgValue=mapnode[availableStep[i]/mapWidth][availableStep[i]%mapWidth];
				nodesInstance[availableStep[i]/mapWidth][availableStep[i]%mapWidth].setBackgroundImage(bgValue);
			}
		}
		/*for (int i = 0; i < mapHeight; i++) {
			for (int j = 0; j < mapWidth; j++) {
				nodesInstance[mapHeight][mapWidth].revalidate();
			}
		}*/
	}
//useless can delete
	public boolean setGrayGrid(int curJ, int curI, int curPosX, int curPosY) {
		int distantCount = 0;
		if (curPosX > curJ)
			distantCount += curPosX - curJ;
		else
			distantCount += curJ - curPosX;

		if (curPosY > curI)
			distantCount += curPosY - curI;
		else
			distantCount += curI - curPosY;

		if (distantCount <= 3)
			return true;
		else
			return false;
	}

	public int getNodeNum() {
		return nodeNum;
	}

	public int getMapWidth() {
		return mapWidth;
	}

	public int getMapHeight() {
		return mapHeight;
	}

	public void setMapWidth(int w) {
		this.mapWidth = w;
	}

	public void setMapHeight(int h) {
		this.mapHeight = h;
	}
	public void setCurPlayerOnNode(int playerPos,int playerRole){
		nodesInstance[playerPos/mapWidth][playerPos%mapWidth].setIconState(playerNodeStyle[playerRole]);
	}
	
	/**remove the player icon of current player on the original map node {@link playerPos}
	 * @param  playerPos  position of player before move to next mapnode
	 */
	public void removeCurPlayerOnNode(int playerPos){
		if(eventPos[playerPos/mapWidth][playerPos%mapWidth]==1)
			nodesInstance[playerPos/mapWidth][playerPos%mapWidth].setIconState(EventNodeStyle);
		else 
			nodesInstance[playerPos/mapWidth][playerPos%mapWidth].setIconState(null);
	}
	public boolean isNodeContainEvent(int NodeIndex){
		if (eventPos[NodeIndex/mapWidth][NodeIndex%mapWidth]==1) {
			return true;
		}
		return false;
	}
	public boolean isNodeCanGo(int NodeIndex){
		for (int i = 0; i < 3; i++) {
			if (availableStep[i]==NodeIndex)
				return true;
		}
		return false;
	}
}

/*
 * Hide details Change log r41 by ckitsa@gmail.com on Today (moments ago) Diff
 * setGrayGrid() Go to: Double click a line to add a comment Older revisions r6
 * by lamkityung@gmail.com on Mar 6, 2012 Diff All revisions of this file File
 * info Size: 2135 bytes, 95 lines View raw file
 */
/*----------------Version detail----------------------------

Modified time: 13:00_11/04/2012

Editor: Lam Kit Yung

Content: 

Create new MapNode class
Add Function:
public int getIndex();

Modify Constructor:

-----------------------------------------------------------

*/


